//
//  XYHomeCellF.swift
//  SinaWB_XY
//
//  Created by 小熊猫 on 15/10/17.
//  Copyright © 2015年 小熊猫. All rights reserved.
//

import UIKit

//间距
private let CELL_MARGIN:CGFloat = 10
//用户姓名的字体大小
let CELL_NAME_FONT:CGFloat = 14
//时间和来源的的字体大小
let CELL_TIME_SOURCE_FONT:CGFloat = 12
//微博内容的字体大小
let CELL_TEXT_FONT:CGFloat = 15

class XYHomeCellF: NSObject {

    var status:XYStatusesModel?{
        didSet{
            setSubViewsFrame()
        }
    }
    
    //整个cell的高度
    var rowHeight:CGFloat = 0
    
    //整个原创微博的frame
    var originalViewF: CGRect?
    
    //用户头像的frame
    var userProfile_imageF:CGRect?
    
    //用户姓名的frame
    var userNameF: CGRect?
    
    //会员图标frame
    var vipImageF:CGRect?
    
    //微博创建时间的frame
    var createdTimeF: CGRect?
    
    //创建微博的来源frame
    var sourceF:CGRect?
    
    //原创微博的配图
    var originateImageF:CGRect?
    
    //原创微博内容的frame
    var originateTextF: CGRect?
    
    //转发微博的整个view
    var retweetedViewF:CGRect?
    
    //转发微博的内容
    var retweetedTextF:CGRect?
    
    //转发微博的配图
    var retweetedImageF:CGRect?
    
    //底部ToolBar的frame
    var toolBarF:CGRect?
    
    //计算各个控件的frame
    private func setSubViewsFrame(){
        //从frame模型的属性中.取出数据模型
        let status = self.status!
        
        
        //给头像计算frame
        let userProfile_imageX = CELL_MARGIN
        let userProfile_imageY = CELL_MARGIN
        let userProfile_imageSize = CGSizeMake(35, 35)
        userProfile_imageF = CGRect(origin: CGPointMake(userProfile_imageX, userProfile_imageY), size: userProfile_imageSize)
        
        //给姓名计算frame
        let userNameX = CGRectGetMaxX(userProfile_imageF!) + CELL_MARGIN
        let userNameY = userProfile_imageY
        let userNameSize = status.user!.name!.size(UIFont.systemFontOfSize(CELL_NAME_FONT))
        userNameF = CGRect(origin: CGPointMake(userNameX, userNameY), size: userNameSize)
        
        //给会员图标计算
        if status.user!.isVip{
            
            let vipImageX = CGRectGetMaxX(userNameF!) + CELL_MARGIN
            let vipImageY = userNameY
            let vipImageSize = CGSizeMake(userNameSize.height, userNameSize.height)
            vipImageF = CGRect(origin: CGPointMake(vipImageX, vipImageY), size: vipImageSize)
        }
        
        //给创建时间计算frame
        let createdTimeX = userNameX
        let createdTimeSize = status.created_atStr.size(UIFont.systemFontOfSize(CELL_TIME_SOURCE_FONT))
        let createdTimeY = CGRectGetMaxY(userProfile_imageF!) - createdTimeSize.height
        createdTimeF = CGRect(origin: CGPointMake(createdTimeX, createdTimeY), size: createdTimeSize)
        
        //计算创建微博来源的frame
        let sourceX = CGRectGetMaxX(createdTimeF!) + CELL_MARGIN
        let sourceY = createdTimeY
        let sourceSize = status.source!.size(UIFont.systemFontOfSize(CELL_TIME_SOURCE_FONT))
        sourceF = CGRect(origin: CGPointMake(sourceX, sourceY), size: sourceSize)
        
        //计算原创微博内容frame
        let originateTextX = CELL_MARGIN
        let originateTextY = CGRectGetMaxY(userProfile_imageF!) + CELL_MARGIN
        let originateTextSize = status.text!.size(UIFont.systemFontOfSize(CELL_TEXT_FONT), constrainedToSize: CGSizeMake(SCREENW - 2 * CELL_MARGIN, CGFloat(MAXFLOAT)))
        originateTextF = CGRect(origin: CGPointMake(originateTextX, originateTextY), size:originateTextSize)
        
        //计算整个原创微博的高
        var originalViewH = CGRectGetMaxY(originateTextF!) + CELL_MARGIN
        
        //计算原创微博配图view大小
        if let picImage = status.pic_urls where picImage.count != 0{
            let originateImageX = CELL_MARGIN
            let originateImageY = CGRectGetMaxY(originateTextF!) + CELL_MARGIN
            let originateImageSize = XYOriginateView.size(picImage.count)
            
            originateImageF = CGRect(origin: CGPointMake(originateImageX, originateImageY), size:originateImageSize)
            
            originalViewH = CGRectGetMaxY(originateImageF!) + CELL_MARGIN
        }
        
        //计算原创微博view的frame

        originalViewF = CGRectMake(0, CELL_MARGIN, SCREENW, originalViewH)
        
        //计算底部toolBar的frame
        var  toolBarY = CGRectGetMaxY(originalViewF!)
        
        //计算转发微博的内容
        if let retweeted = status.retweeted_status {
            let  retweetedViewX:CGFloat = 0
            let retweetedViewY = CGRectGetMaxY(originalViewF!)
            let retweetedViewW = SCREENW
            
            //在里面计算转发内容的大小
            let retweetedTextX = CELL_MARGIN
            let retweetedTextY = CELL_MARGIN
            let retweetedTextSize = retweeted.text!.size(UIFont.systemFontOfSize(CELL_TEXT_FONT), constrainedToSize: CGSizeMake(SCREENW - 2 * CELL_MARGIN, CGFloat(MAXFLOAT)))
            retweetedTextF = CGRect(origin: CGPointMake(retweetedTextX, retweetedTextY), size:retweetedTextSize)
            var  retweetedViewH = CGRectGetMaxY(retweetedTextF!) + CELL_MARGIN
            
            //计算转发未必配图的frame
            if let retweetedPic = retweeted.pic_urls where retweetedPic.count != 0 {
                let retweetedImageX = CELL_MARGIN
                let retweetedImageY = CGRectGetMaxY(retweetedTextF!) + CELL_MARGIN
                let retweetedImageSize = XYOriginateView.size(retweetedPic.count)
                retweetedImageF = CGRect(origin: CGPointMake(retweetedImageX, retweetedImageY), size:retweetedImageSize)
                
                retweetedViewH = CGRectGetMaxY(retweetedImageF!) + CELL_MARGIN
            }
            
            retweetedViewF = CGRect(origin: CGPointMake(retweetedViewX, retweetedViewY), size:CGSizeMake(retweetedViewW, retweetedViewH))
            
            toolBarY = CGRectGetMaxY(retweetedViewF!)
        }
        
        
        toolBarF = CGRectMake(0, toolBarY, SCREENW, 35)
        
        //计算行高
        rowHeight = CGRectGetMaxY(toolBarF!)
        
    }
    
    
}
